package mysql

import (
	"videos/videorpc/videorpc"

	"gorm.io/gorm"
)

// 收藏

type Collection struct {
	gorm.Model
	UserId  int `gorm:"type:int;not null;comment:用户ID"`
	VideoId int `gorm:"type:int;not null;comment:视频ID"`
}

// 收藏用户视频

type CollectionUserVideo struct {
	CollectionId int
	NickName     string
	Title        string
}

// 查询收藏用户视频

func (v *CollectionUserVideo) CollectionDetail(db *gorm.DB, id int64) error {
	return db.Model(&Collection{}).
		Select(
			"collections.id as CollectionId",
			"users.nick_name as nick_name",
			"videos.title as title",
		).
		Joins("left join users on users.id = collections.user_id").
		Joins("left join videos on videos.id = collections.video_id").
		Debug().Where("collections.id = ?", id).Find(v).Error
}

// 收藏

func (c *Collection) CollectionAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

// 取消收藏

func (c *Collection) CollectionDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("collections.id=?", id).Delete(c).Error
}

// 收藏列表

func CollectionList(db *gorm.DB, in *videorpc.CommentListRequest) []*videorpc.CollectionList {
	/*
		select
		collections.id as CollectionId,
		users.nick_name as nick_name,
		videos.title as title
		from collections
		left join users on users.id = collections.user_id
		left join videos on videos.id = collections.video_id
	*/
	var list []*videorpc.CollectionList

	db.Model(&Collection{}).
		Select(
			"collections.id as CollectionId",
			"users.nick_name as nick_name",
			"videos.title as title",
		).
		Joins("left join users on users.id = collections.user_id").
		Joins("left join videos on videos.id = collections.video_id").
		Debug().Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)

	return list
}
